Imports Phoenix.DataAccess

Namespace Forms

    Public Class ProductForm : Inherits ChildFormBase

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Panel1 As System.Windows.Forms.Panel
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents lblStock As System.Windows.Forms.Label
        Friend WithEvents lblPrice As System.Windows.Forms.Label
        Friend WithEvents lblFamily As System.Windows.Forms.Label
        Friend WithEvents lblType As System.Windows.Forms.Label
        Friend WithEvents lblLabel As System.Windows.Forms.Label
        Friend WithEvents FamilyCombo As DevExpress.XtraEditors.ComboBoxEdit
        Friend WithEvents TypeCombo As DevExpress.XtraEditors.ComboBoxEdit
        Friend WithEvents LabelBox As DevExpress.XtraEditors.TextEdit
        Friend WithEvents lblSt As System.Windows.Forms.Label
        Friend WithEvents CommentMemoEdit As DevExpress.XtraEditors.MemoEdit
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents Belevel2 As System.Windows.Forms.GroupBox
        Friend WithEvents PriceBox As DevExpress.XtraEditors.CalcEdit
        Friend WithEvents StockBox As DevExpress.XtraEditors.SpinEdit
        Friend WithEvents CriticalStockBox As DevExpress.XtraEditors.SpinEdit
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(ProductForm))
            Me.Panel1 = New System.Windows.Forms.Panel
            Me.CriticalStockBox = New DevExpress.XtraEditors.SpinEdit
            Me.StockBox = New DevExpress.XtraEditors.SpinEdit
            Me.PriceBox = New DevExpress.XtraEditors.CalcEdit
            Me.Belevel2 = New System.Windows.Forms.GroupBox
            Me.CommentMemoEdit = New DevExpress.XtraEditors.MemoEdit
            Me.lblSt = New System.Windows.Forms.Label
            Me.Label1 = New System.Windows.Forms.Label
            Me.lblPrice = New System.Windows.Forms.Label
            Me.lblFamily = New System.Windows.Forms.Label
            Me.lblType = New System.Windows.Forms.Label
            Me.FamilyCombo = New DevExpress.XtraEditors.ComboBoxEdit
            Me.TypeCombo = New DevExpress.XtraEditors.ComboBoxEdit
            Me.lblLabel = New System.Windows.Forms.Label
            Me.LabelBox = New DevExpress.XtraEditors.TextEdit
            Me.Panel1.SuspendLayout()
            CType(Me.CriticalStockBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.StockBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.PriceBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.CommentMemoEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.FamilyCombo.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.TypeCombo.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.LabelBox.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'Panel1
            '
            Me.Panel1.Controls.Add(Me.CriticalStockBox)
            Me.Panel1.Controls.Add(Me.StockBox)
            Me.Panel1.Controls.Add(Me.PriceBox)
            Me.Panel1.Controls.Add(Me.Belevel2)
            Me.Panel1.Controls.Add(Me.CommentMemoEdit)
            Me.Panel1.Controls.Add(Me.lblSt)
            Me.Panel1.Controls.Add(Me.Label1)
            Me.Panel1.Controls.Add(Me.lblPrice)
            Me.Panel1.Controls.Add(Me.lblFamily)
            Me.Panel1.Controls.Add(Me.lblType)
            Me.Panel1.Controls.Add(Me.FamilyCombo)
            Me.Panel1.Controls.Add(Me.TypeCombo)
            Me.Panel1.Controls.Add(Me.lblLabel)
            Me.Panel1.Controls.Add(Me.LabelBox)
            Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
            Me.Panel1.Location = New System.Drawing.Point(0, 49)
            Me.Panel1.Name = "Panel1"
            Me.Panel1.Size = New System.Drawing.Size(640, 357)
            Me.Panel1.TabIndex = 4
            '
            'CriticalStockBox
            '
            Me.CriticalStockBox.EditValue = 0
            Me.CriticalStockBox.Location = New System.Drawing.Point(128, 216)
            Me.CriticalStockBox.Name = "CriticalStockBox"
            '
            'CriticalStockBox.Properties
            '
            Me.CriticalStockBox.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton})
            Me.CriticalStockBox.Properties.IsFloatValue = False
            Me.CriticalStockBox.Properties.UseCtrlIncrement = False
            Me.CriticalStockBox.Size = New System.Drawing.Size(80, 20)
            Me.CriticalStockBox.TabIndex = 30
            '
            'StockBox
            '
            Me.StockBox.EditValue = 0
            Me.StockBox.Location = New System.Drawing.Point(128, 184)
            Me.StockBox.Name = "StockBox"
            '
            'StockBox.Properties
            '
            Me.StockBox.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton})
            Me.StockBox.Properties.IsFloatValue = False
            Me.StockBox.Properties.UseCtrlIncrement = False
            Me.StockBox.Size = New System.Drawing.Size(80, 20)
            Me.StockBox.TabIndex = 29
            '
            'PriceBox
            '
            Me.PriceBox.Location = New System.Drawing.Point(88, 136)
            Me.PriceBox.Name = "PriceBox"
            '
            'PriceBox.Properties
            '
            Me.PriceBox.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.PriceBox.Properties.DisplayFormat.FormatString = "c2"
            Me.PriceBox.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.PriceBox.Properties.EditFormat.FormatString = "f2"
            Me.PriceBox.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            Me.PriceBox.Properties.Precision = 2
            Me.PriceBox.Size = New System.Drawing.Size(80, 20)
            Me.PriceBox.TabIndex = 28
            '
            'Belevel2
            '
            Me.Belevel2.Location = New System.Drawing.Point(0, 168)
            Me.Belevel2.Name = "Belevel2"
            Me.Belevel2.Size = New System.Drawing.Size(328, 3)
            Me.Belevel2.TabIndex = 27
            Me.Belevel2.TabStop = False
            '
            'CommentMemoEdit
            '
            Me.CommentMemoEdit.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.CommentMemoEdit.EditValue = ""
            Me.CommentMemoEdit.Location = New System.Drawing.Point(336, 8)
            Me.CommentMemoEdit.Name = "CommentMemoEdit"
            Me.CommentMemoEdit.Size = New System.Drawing.Size(296, 340)
            Me.CommentMemoEdit.TabIndex = 7
            '
            'lblSt
            '
            Me.lblSt.Location = New System.Drawing.Point(8, 186)
            Me.lblSt.Name = "lblSt"
            Me.lblSt.Size = New System.Drawing.Size(112, 16)
            Me.lblSt.TabIndex = 26
            Me.lblSt.Text = "&Current Stock Level:"
            Me.lblSt.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(8, 218)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(120, 16)
            Me.Label1.TabIndex = 25
            Me.Label1.Text = "Minimal Stock Level:"
            Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
            '
            'lblPrice
            '
            Me.lblPrice.Location = New System.Drawing.Point(8, 138)
            Me.lblPrice.Name = "lblPrice"
            Me.lblPrice.Size = New System.Drawing.Size(48, 16)
            Me.lblPrice.TabIndex = 21
            Me.lblPrice.Text = "&Price:"
            '
            'lblFamily
            '
            Me.lblFamily.Location = New System.Drawing.Point(8, 98)
            Me.lblFamily.Name = "lblFamily"
            Me.lblFamily.Size = New System.Drawing.Size(56, 16)
            Me.lblFamily.TabIndex = 19
            Me.lblFamily.Text = "&Family:"
            '
            'lblType
            '
            Me.lblType.Location = New System.Drawing.Point(8, 58)
            Me.lblType.Name = "lblType"
            Me.lblType.Size = New System.Drawing.Size(56, 16)
            Me.lblType.TabIndex = 18
            Me.lblType.Text = "&Type:"
            '
            'FamilyCombo
            '
            Me.FamilyCombo.EditValue = ""
            Me.FamilyCombo.Location = New System.Drawing.Point(88, 96)
            Me.FamilyCombo.Name = "FamilyCombo"
            '
            'FamilyCombo.Properties
            '
            Me.FamilyCombo.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.FamilyCombo.Size = New System.Drawing.Size(240, 20)
            Me.FamilyCombo.TabIndex = 3
            '
            'TypeCombo
            '
            Me.TypeCombo.EditValue = ""
            Me.TypeCombo.Location = New System.Drawing.Point(88, 56)
            Me.TypeCombo.Name = "TypeCombo"
            '
            'TypeCombo.Properties
            '
            Me.TypeCombo.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.TypeCombo.Size = New System.Drawing.Size(240, 20)
            Me.TypeCombo.TabIndex = 2
            '
            'lblLabel
            '
            Me.lblLabel.Location = New System.Drawing.Point(8, 18)
            Me.lblLabel.Name = "lblLabel"
            Me.lblLabel.Size = New System.Drawing.Size(48, 16)
            Me.lblLabel.TabIndex = 15
            Me.lblLabel.Text = "&Label:"
            '
            'LabelBox
            '
            Me.LabelBox.EditValue = ""
            Me.LabelBox.Location = New System.Drawing.Point(88, 16)
            Me.LabelBox.Name = "LabelBox"
            Me.LabelBox.Size = New System.Drawing.Size(240, 20)
            Me.LabelBox.TabIndex = 1
            '
            'ProductForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(640, 406)
            Me.Controls.Add(Me.Panel1)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.Name = "ProductForm"
            Me.Text = "New Product"
            Me.Controls.SetChildIndex(Me.Panel1, 0)
            Me.Panel1.ResumeLayout(False)
            CType(Me.CriticalStockBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.StockBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.PriceBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.CommentMemoEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.FamilyCombo.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.TypeCombo.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.LabelBox.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Data "
        Public Shadows Property DataSource() As DataSetData.Product
            Get
                Return CType(MyBase.DataSource, DataSetData.Product)
            End Get
            Set(ByVal Value As DataSetData.Product)
                MyBase.DataSource = Value
            End Set
        End Property
        Protected Overrides Sub DoBinding()
            ' Load Types/Families
            Dim dv As DataView = New DataView(AppDataLayer.Products)

            'Loading types
            dv.Sort = "Type ASC"
            Dim type As String = String.Empty
            TypeCombo.Properties.Items.Clear()
            For Each row As DataRowView In dv
                Dim stype As String = CType(row.Row, DataSetData.Product).Type
                If (stype <> String.Empty) And (stype <> type) Then
                    TypeCombo.Properties.Items.Add(stype)
                    type = stype
                End If
            Next

            'Loading Families
            dv.Sort = "Family ASC"
            Dim family As String = String.Empty
            FamilyCombo.Properties.Items.Clear()
            For Each row As DataRowView In dv
                Dim sfamily As String = CType(row.Row, DataSetData.Product).Family
                If (sfamily <> String.Empty) And (sfamily <> family) Then
                    FamilyCombo.Properties.Items.Add(sfamily)
                    family = sfamily
                End If
            Next


            UpdateSecurityEditCommands()

            ' Binding controls
            Dim bind As Binding

            BindWithCapitalization(LabelBox, "Text", DataSource, "Label")
            'LabelBox.DataBindings.Add("Text", DataSource, "Label")

            BindWithCapitalization(TypeCombo, "Text", DataSource, "Type")
            'TypeCombo.DataBindings.Add("EditValue", DataSource, "Type")

            BindWithCapitalization(FamilyCombo, "Text", DataSource, "Family")
            'FamilyCombo.DataBindings.Add("EditValue", DataSource, "Family")

            PriceBox.DataBindings.Add("EditValue", DataSource, "Price")
            StockBox.DataBindings.Add("EditValue", DataSource, "Stock")
            CriticalStockBox.DataBindings.Add("EditValue", DataSource, "CriticalStock")
            CommentMemoEdit.DataBindings.Add("Text", DataSource, "Comment")
        End Sub

        Public Overrides ReadOnly Property DocumentTitle() As String
            Get
                Return DataSource.Label()
            End Get
        End Property

        Public Overrides ReadOnly Property DocumentType() As String
            Get
                Return Res.TypeProductDocument
            End Get
        End Property

        Protected Overrides Sub UpdateSecurityEditCommands()
            If DeleteBarItem.Enabled Then
                DeleteBarItem.Enabled = Security.CanDeleteProduct
            End If
            SaveBarItem.Enabled = Security.CanCreateProduct
            SaveCloseBarItem.Enabled = Security.CanCreateProduct
            SaveNewBarItem.Enabled = Security.CanCreateProduct

            LabelBox.Properties.ReadOnly = Not (Security.CanCreateProduct())
            TypeCombo.Properties.ReadOnly = Not (Security.CanCreateProduct())
            FamilyCombo.Properties.ReadOnly = Not (Security.CanCreateProduct())
            PriceBox.Properties.ReadOnly = Not (Security.CanCreateProduct())
            StockBox.Properties.ReadOnly = Not (Security.CanCreateProduct())
            CriticalStockBox.Properties.ReadOnly = Not (Security.CanCreateProduct())
            CommentMemoEdit.Properties.ReadOnly = Not (Security.CanCreateProduct())
        End Sub

#End Region

#Region " Life-Cycle Routines "
        Protected Overrides Sub DoSaving()
            'AppDataLayer.Products.AddProduct(DataSource)
            Helpers.ProductHelper.UpdateProduct(DataSource)
        End Sub

        Protected Overrides Function CreateNewRow() As DataRow
            Dim pr As DataSetData.Product = AppDataLayer.Products.NewProduct
            pr.Label = Res.NewProductDefaultLabel
            Return pr
        End Function

        Protected Overrides Sub OnDelete()
            Helpers.ProductHelper.DeleteProduct(DataSource)
        End Sub

#End Region

    End Class
End Namespace